import bge, bpy, bgl, blf, mathutils, aud, os
from time import sleep
import os
from .ressources import *

CORE = bge.core
IN = CORE.inputs
UI = CORE.gui
SZ_Globals = CORE.SZ_Globals

class MultiStructUi:
	def __init__(self, parentLayer, UI, globVars, **kargs):
		self.layer=parentLayer
		self.globVars=globVars
		self.UI=UI
		#~ soloPanel = UI.ButtonList(parentLayer, 0,"i0",("Continuer","Commencer","Sauver","Charger","Carte perso"),1.0)
		#~ soloPanel = UI.CheckBox(parentLayer, 0,"i0", 'value', 'print(stat)', 'Test')
		self.LabelText = UI.TextBox(parentLayer, 0.5, "i0.5", "Connexion en cours...")
		#~ schema=[[{'width':0.2},{'width':0.2}], [['L0C0', {}],['L0C1', {}],['L0C2', {}]],[['L1C0', {}],['L1C1', {}],['L1C2', {}]]]
		#~ soloPanel = UI.Table(parentLayer, 0,"i0", 1.0,False, schema)
		self.LabelText.visible = True
		#~ soloPanel._onclic = self.serverSelect
		
	def serverSelect(self):	
		pass
	
	def load(self):
		self.LabelText.visible=False
		schema=[[{'width':50, 'label': 'Id'},{'width':250, 'label': 'Nom de la partie'},{'width':100, 'label': 'Type'}], [['1', {}],['Partie de Test', {'onclick':'print("ok")'}],['DeathMatch', {}]],[['2', {}],['DevMap', {}],['DeathMatch', {}]]]
		self.searchBar = self.UI.InputField(self.layer, 0,"i2", 'test', self.globVars['MainInputMap'], self.updateFilter)
		self.privateGame = self.UI.CheckBox(self.layer, 0+self.searchBar.w+10,"i2", 'val', self.updateFilter, 'Partie Privé')
		self.serverGrid = self.UI.Table(self.layer, 0,"i"+str(self.searchBar.h+4), 1.0,False, schema)
		
	def updateFilter(self):
		print({'privateGame': self.privateGame.isChecked(), 'keyWord': self.searchBar.getText()})
		
	def connectServer(self, ip):
		pass

	def winServer(self, ip):			
		SZ_Globals['2DScreen'].setLayer('serverLink')
		mask = UI.Zone(self.globVars['2DScreen'],0,0,1.0,1.0)
		mask.color = transblack
		bar = UI.Zone(mask,335,"i300",5,350)
		bar.color = col_border
		textip = UI.TextBox(mask, 348, "i300", "Préparation de partie:")
		textip.text_color = col_border
		textip.size = 28
		textip.font = font
		
		### Waiting callback from mainserver class
		schema=[[{'width':250, 'label': 'Nom du joueue'},{'width':100, 'label': 'Pays'},{'width':200, 'label': 'Rang'},{'width':200, 'label': 'Activite'},{'width':100, 'label': 'Status'}], [['Antoine', {}],['France', {}],['Niv. 13 - Vétéran', {}],['2heures 6 minutes', {}],['Prét(e)', {}]],[['Gost', {}],['Siberie', {}],['Niv. 99 - Dieu', {}],['plus de 10heures', {}],['Prét(e)', {}]]]
		#~ schema=[[{'width':250, 'label': 'Nom du joueue'},{'width':100, 'label': 'Pays'},{'width':100, 'label': 'Activité'},{'width':100, 'label': 'Rang'}], [['Antoine', {}],['France', {'background': 'resources/textures/multi/flag/france'}],['Niv. 13 - Vétéran', {}],['2heures 6 minutes', {}]],[['Gost', {}],['Siberie', {}],['Niv. 99 - Dieu', {}],['plus de 10heures', {}]]]
		serverGrid = UI.Table(mask, 348,"i340", 0.8,False, schema)
		
		textip = UI.TextBox(mask, 348, "i{}".format(350+serverGrid.decY), "Infos sur la partie:")
		textip.text_color = col_border
		textip.size = 24
		textip.font = font
		### Waiting callback from mainserver class
		schema=[[['Type:', {'width':130}],['DeathMatch', {}]],[['Association:', {'width':130}],['En Equipe', {}]],[['Joueurs Max.:', {'width':130}],['32', {}]],[['Statistique', {'width':130}],['Activé', {}]]]
		#~ schema=[[{'width':250, 'label': 'Nom du joueue'},{'width':100, 'label': 'Pays'},{'width':100, 'label': 'Activité'},{'width':100, 'label': 'Rang'}], [['Antoine', {}],['France', {'background': 'resources/textures/multi/flag/france'}],['Niv. 13 - Vétéran', {}],['2heures 6 minutes', {}]],[['Gost', {}],['Siberie', {}],['Niv. 99 - Dieu', {}],['plus de 10heures', {}]]]
		infoGrid = UI.Table(mask, 348,"i{}".format(350+serverGrid.decY+textip.h), 0.45,False, schema)
		forceStartLab = UI.TextBox(mask, 348, "i530", "Démarage forcé dans {}".format('1 minute 57'))
		forceStartLab.text_color = UI.Color((220/255,46/255,14/255))
		forceStartLab.size = 18
		forceStartLab.font = font
		### mapname
		mapImg=MultiFunction.loadMapPreview('qg')
		previewMapLab = UI.TextBox(mask, 800, "i{}".format(350+serverGrid.decY), "Prochaine carte:")
		previewMapLab.text_color = col_border
		previewMapLab.size = 22
		previewMapLab.font = font
		frameMap=UI.Box(mask, 800+(previewMapLab.h/2), "i{}".format(350+serverGrid.decY+previewMapLab.h),150,mapImg.height/(mapImg.width/150),mapImg)
		MapNameLab = UI.TextBox(mask, 810+(previewMapLab.h/2)+frameMap.w, "i{}".format(425+serverGrid.decY+previewMapLab.h), "qg")
		MapNameLab.text_color = col_border
		MapNameLab.size = 24
		MapNameLab.font = font
		
		statusLabel=UI.TextBox(mask, 348, "i600", "En attente du téléchargement...")
		statusLabelProgress=UI.TextBox(mask, 348+statusLabel.w, "i600", "100%")
		statusLabel.text_color= col_border
		statusLabelProgress.text_color= col_border
		statusLabel.size = 18
		statusLabelProgress.size = 18
		statusLabel.font = font
		statusLabelProgress.font = font
		statusProgress=UI.LevelBar(mask, 348, "i630", 600, 20)
		statusProgress.bar.color = UI.Color((0.478431373,.0,0.0))
		statusProgress.level=100
		buttonquit = UI.Button(mask, 348,"i655",'Quitter')
		buttonquit.text_color = col_border
		buttonquit.size = 20
		buttonquit.font = font
		buttonquit.hover_modification["color"] = col_border
		buttonquit.hover_modification["text_color"] = black
		buttonquit._onclic = self.leaveGame
				
	def leaveGame(self, arg=False):
		SZ_Globals['2DScreen'].hideLayer('serverLink')
		SZ_Globals['2DScreen'].setLayer('menu')
class MultiFunction:
	def loadMapPreview(name):
		if os.path.exists(bpy.path.abspath('//resources/textures/multi/cache/preview_map_{}.png'.format(name))):
			return UI.Image('//resources/textures/multi/cache/preview_map_{}.png'.format(name))
		else:
			return UI.Image('//resources/textures/multi/cache/loading.png')
